const Blog = require("../models/Blog")
const Class = require("../models/Class")
const sequelize = require("../models/boke");

Blog.belongsToMany(Class, {
  through: 'Link'
});
Class.belongsToMany(Blog, {
  through: 'Link'
});

// 添加标签
exports.addClass = async function (classText) {
  const result = await Class.findOne({
    where: {
      labelClass: classText
    }
  })
  if (result) {
    return {
      code : 1,
      classText : result.labelClass
    } //标签重复
  }
  const results = await Class.create({
    labelClass: classText
  })
  if (results) {
    return {
      code : 2,
      classText : results.labelClass
    } //添加成功
  }

}

// addClass()


exports.getClass = async function () {
  const result = await Class.findAll();
  return result;
}

exports.getclassify = async function (obj) {
  const result = await Class.findOne({
    where: {
      id: obj.id,
    },
    include: [Blog],
  })
  return result;
}

exports.getclassify = async function (obj) {
  const result = await Class.findOne({
    where: {
      id: obj.id,
    },
    include: [Blog],
  })
  return result;
}

exports.Statistics = async function () {
  const result = await Class.findAll({
    include: [Blog],
  })
  const results = JSON.parse(JSON.stringify(result))
  let arr = [];
  for (let i = 0; i < results.length; i++) {
    if (results[i].Blogs.length > 0) {
      arr.push({
        value: results[i].Blogs.length,
        name: results[i].labelClass
      })
    }
  }
  return arr;
}